/**
 * Created by Jerry on 16/2/1.
 */
laAir.controller('laAir_SearchCtl', ['$window', '$document', '$scope','$interval','$filter', 'laUserService', 'laFlightService', 'laGlobalLocalService',function ($window, $document, $scope,$interval,$filter,laUserService, laFlightService, laGlobalLocalService) {
    //去程预定查询信息
    $scope.queryFliInfo = new laEntityFlight();
    //返程预定查询信息
    $scope.queryFliInfoBack = new laEntityFlight();
    $scope.FromCity = "杭州";
    $scope.ToCity = "西安";
    var today = new Date();
    today.setTime(today.getTime()+24*60*60*1000);
    $scope.CityList={};
    $scope.showCity=false;
    $scope.StartTime = $filter('date')(today, 'yyyy-MM-dd');
    $scope.EndTime = $filter('date')(today.setTime(today.getTime()+2*24*60*60*1000), 'yyyy-MM-dd');
    $scope.SearchPageProperty={
        Type:"Function",
        Id:"pageSearch",
        name:"城市选择",
        Url:"http://172.25.1.35:200/citypage.html",
        Serial:"",
        ToFrame:"main",
        HasFoot:false
    }
    $scope.PostCookie={};
    $scope.qu_mode=0;
    $scope.RoundTrip=0;
    $scope.StartWeek=laGlobalLocalService.getWeekName(new Date($scope.StartTime.replace(/-/g, "/")));
    $scope.EndWeek=laGlobalLocalService.getWeekName(new Date($scope.EndTime.replace(/-/g, "/")));
    laGlobalLocalService.postFrameUrl(location.href);
    laUserService.QueryAirportInfo("", function (backData, status) {
        $scope.CityList = backData.Result;
    });

    var defCity = {"s": {"c": "HGH", "n": "杭州"}, "e": {"c": "CAN", "n": "广州"}};
    $("#startCity").attr("segnum", defCity.s.c);
    $("#startCity").val(defCity.s.n);
    $("#arriveCity").attr("segnum", defCity.e.c);
    $("#arriveCity").val(defCity.e.n);
    $scope.CityFlag=0;
    $scope.ChooseCityBtn=function (v) {
        /*if(navigator.onLine){
            $scope.QueryCityTrans={
                FromCity:$("#startCity").val(),
                FromCityCode:$("#startCity").attr("segnum"),
                ToCity:$("#endCity").val(),
                ToCityCode:$("#endCity").attr("segnum"),
            }
            if(v==0){
                $scope.QueryCityTrans.FromCity="";
            }else {
                $scope.QueryCityTrans.ToCity="";
            }
            $scope.PostCookie=JSON.parse(laGlobalLocalService.toFullScreen($scope.SearchPageProperty));
            location.href="http://172.25.1.35:200/citypage.html?param=" + new Base64().encode(JSON.stringify($scope.QueryCityTrans));
        }else{
            alert("没有网络！")
        }*/
        $scope.CityFlag=v;
        laGlobalLocalService.toFullScreen($scope.SearchPageProperty);
        $scope.showCity=true;
    }
    $scope.chooseRoundTrip = function (v) {
        $scope.qu_mode=v;
        $scope.RoundTrip=v;
        if(v==0){
            $(".dongtaiByNo").css("color","#FF0000");
            $(".dongtaiByCity").css("color","#333333");
        }else if(v==1){
            $(".dongtaiByCity").css("color","#FF0000");
            $(".dongtaiByNo").css("color","#333333");
        }
    };
    $scope.SearchPageProperty={
        Type:"Function",
        Id:"pageSearch",
        name:"城市选择",
        Url:"http://172.25.1.35:200/citypage.html",
        Serial:"",
        ToFrame:"main",
        HasFoot:false,
        Height:"240px"
    }
    $scope.chooseCity=function (CityInfo) {
        $scope.showCity=false;
        laGlobalLocalService.toScreen($scope.SearchPageProperty);
        if($scope.CityFlag==0){
            $("#startCity").attr("segnum", CityInfo.AirportCode);
            $("#startCity").val(CityInfo.CityName);
        }else {
            $("#arriveCity").attr("segnum", CityInfo.AirportCode);
            $("#arriveCity").val(CityInfo.CityName);
        }
    }
    $window.addEventListener('message', function(e) {
        filter(e.data);
    }, false);
    //广播过滤器
    function filter(data) {

        if (data.To.Target == "monitorKey") {

            if (data.Type == "Function") {
                distribute(data, data.To.Func, data.Content);
            } else if (data.Type == "Data") {
            }
        }
    }
    //广播分发器
    function distribute(data, func, content) {
        if(func=="backKey"){
            laGlobalLocalService.toScreen($scope.SearchPageProperty);
        }else {
            return false;
        }
    }
    /**
     * 搜索机票按钮点击事件
     */
    $scope.btnQueryTicketClick = function () {
        var sCity = $("#startCity").val();
        var eCity = $("#arriveCity").val();
        //var sCityCode = $("#startCity").attr("segnum");
        //var eCityCode = $("#endCity").attr("segnum");
        var sCityCode = $("#startCity").attr("segnum");
        var eCityCode = $("#arriveCity").attr("segnum");
        var sTime = $("#StartTime").val();
        var eTime = $("#EndTime").val();

        $scope.queryFliInfo.AirportFromCH = sCity;
        $scope.queryFliInfo.AirportToCH = eCity;
        $scope.queryFliInfo.AirportFrom = sCityCode;
        $scope.queryFliInfo.AirportTo = eCityCode;
        $scope.queryFliInfo.DepartureTime = sTime;
        $scope.queryFliInfo.RoundTripTime = eTime;

        //出发城市为空
        if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.AirportFromCH)) {
            alert('请选择出发城市');
            return;
        }
        //到达城市为空
        if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.AirportToCH)) {
            alert('请选择到达城市');
            return;
        }
        if ($scope.queryFliInfo.AirportFrom == $scope.queryFliInfo.AirportTo) {
            alert('出发和到达城市不能相同');
            return;
        }

        //出发时间为空
        if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.DepartureTime)) {
            alert('请选择出发时间');
            return;
        }
        if (!laGlobalLocalService.CheckDateFormat($scope.queryFliInfo.DepartureTime)) {
            alert('请输入YYYY-MM-DD格式的日期');
            return;
        }
        //返程时间为空
        if ($scope.qu_mode == 1) {
            if (laGlobalLocalService.CheckStringIsEmpty($scope.queryFliInfo.RoundTripTime)) {
                alert('请选择返程时间');
                return;
            }
            if (!laGlobalLocalService.CheckDateFormat($scope.queryFliInfo.RoundTripTime)) {
                alert('请输入YYYY-MM-DD格式的日期');
                return;
            }
            if ($scope.queryFliInfo.RoundTripTime < $scope.queryFliInfo.DepartureTime) {
                alert('返程时间不能在去程时间之前');
                return;
            }
            $scope.queryFliInfoBack.AirportFromCH = $scope.queryFliInfo.AirportToCH;
            $scope.queryFliInfoBack.AirportToCH = $scope.queryFliInfo.AirportFromCH;
            $scope.queryFliInfoBack.AirportFrom = $scope.queryFliInfo.AirportTo;
            $scope.queryFliInfoBack.AirportTo = $scope.queryFliInfo.AirportFrom;
            $scope.queryFliInfoBack.DepartureTime = $scope.queryFliInfo.RoundTripTime;
        }
        if($scope.qu_mode==0){
            $scope.queryFliInfo.RoundTrip=false;
        }else {
            $scope.queryFliInfo.RoundTrip=true;
        }
        laGlobalLocalService.setLocalCache(laGlobalProperty.laServiceConst_TransData_QueryTicket, JSON.stringify($scope.queryFliInfo));

        $scope.city1name = sCity;
        $scope.city2name = eCity;
        var PageData={
            Type:"Function",
            name:"航班列表",
            ToFrame:"main",
            Id:"PageAirlineList",
            Url:"http://172.25.1.35:200/airlineListPage.html",
            Serial:"",}
        laGlobalLocalService.gotoNewFrame(PageData)

        /*QueryTicket($scope.queryFliInfo);*/
        /*if($scope.qu_mode == 1){
            setTimeout(QueryTicketBack($scope.queryFliInfoBack),5000);
        }*/
    };
    function QueryTicket(queryFliInfo) {
        if(queryFliInfo.DepartureTime!=undefined){
            var departureTime = new Date(queryFliInfo.DepartureTime.replace(/-/g, "/"));
            var mm=$filter('date')(departureTime, 'MM-dd');
            queryFliInfo.DepartureTimeMonthDay = mm.substring(0,2)+"月"+mm.substring(3,6)+"日";
            queryFliInfo.DepartureTimeWeek = laGlobalLocalService.getWeekName(departureTime);
        }
        $scope.flightResult = null;
        laFlightService.QueryFlight(queryFliInfo, function (backData, status) {
            var rs = backData;
            if (rs.Code == laGlobalProperty.laServiceCode_Success) {
               var PageData={
                    Type:"Function",
                    name:"航班列表",
                    ToFrame:"main",
                    Id:"PageAirlineList",
                    Url:"http://172.25.1.35:200/airlineListPage.html",
                    Serial:"",}
                laGlobalLocalService.gotoNewFrame(PageData)

               /* $interval.cancel(timer);*/

                //$scope.flightResult = rs;
               /* $scope.flightResult = new laEntityFlightList();
                $scope.flightResult.Code = rs.Code;
                $scope.flightResult.Message = rs.Message;
                $scope.flightResult.LowPriceFlights = rs.LowPriceFlights;
                var n = rs.FlightList.length;
                var cabinNum = 0;
                for (var i = 0; i < n; i++) {
                    if (rs.FlightList[i].CabinInfoList.length > 0) {
                        $scope.flightResult.FlightList[cabinNum] = rs.FlightList[i];
                        var date1 = new Date(rs.FlightList[i].DepartureTime);  //开始时间
                        var date2 = new Date(rs.FlightList[i].ArriveTime);     //结束时间
                        var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数
                        //计算相差的年数
                        var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
                        //计算相差的月数
                        var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
                        var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
                        //计算出相差天数
                        var leave0 = leave % (30 * 24 * 3600 * 1000);
                        var days = Math.floor(leave0 / (24 * 3600 * 1000));
                        //计算出小时数
                        var leave1 = leave0 % (24 * 3600 * 1000);     //计算天数后剩余的毫秒数
                        var hours = Math.floor(leave1 / (3600 * 1000));
                        //计算相差分钟数
                        var leave2 = leave1 % (3600 * 1000);         //计算小时数后剩余的毫秒数
                        var minutes = Math.floor(leave2 / (60 * 1000));
                        //计算相差秒数
                        var leave3 = leave2 % (60 * 1000);       //计算分钟数后剩余的毫秒数
                        var seconds = Math.round(leave3 / 1000);
                        $scope.flightResult.FlightList[cabinNum].flighttimelist=hours+"小时"+minutes+"分钟";
                        cabinNum++;
                    }
                }*/

            } else {
            }
           /* GetFlightDaylist(queryFliInfo.AirportFrom, queryFliInfo.AirportTo, new Date(queryFliInfo.DepartureTime.replace(/-/g, "/")));*/
        });
    }
    /**
     * 查询返程机票
     * @constructor
     */
    function QueryTicketBack(queryFliInfo) {
        $(".noflightsBack").hide();
        $(".airlineBack").hide();
        $(".loadingBack").show();
        $(".datesBack").hide();
        if(queryFliInfo.DepartureTime!=undefined){
            var departureTime = new Date(queryFliInfo.DepartureTime.replace(/-/g, "/"));
            var mm=$filter('date')(departureTime, 'MM-dd');
            queryFliInfo.DepartureTimeMonthDay = mm.substring(0,2)+"月"+mm.substring(3,6)+"日";
            queryFliInfo.DepartureTimeWeek = laGlobalLocalService.getWeekName(departureTime);
        }


        $scope.flightBackResult = null;

        laFlightService.QueryFlight(queryFliInfo, function (backData, status) {
            var rs = backData;
            if (rs.Code == laGlobalProperty.laServiceCode_Success) {





                $(".loadingBack").hide();
                $(".noflightsBack").hide();
                $(".airlineBack").show();
                $(".datesBack").show();

                //$scope.flightResult = rs;
                $scope.flightBackResult = new laEntityFlightList();
                $scope.flightBackResult.Code = rs.Code;
                $scope.flightBackResult.Message = rs.Message;
                $scope.flightBackResult.LowPriceFlights = rs.LowPriceFlights;
                var n = rs.FlightList.length;
                var cabinNum = 0;
                for (var i = 0; i < n; i++) {
                    if (rs.FlightList[i].CabinInfoList.length > 0) {
                        $scope.flightBackResult.FlightList[cabinNum] = rs.FlightList[i];
                        var date1 = new Date(rs.FlightList[i].DepartureTime);  //开始时间
                        var date2 = new Date(rs.FlightList[i].ArriveTime);     //结束时间
                        var date3 = date2.getTime() - date1.getTime();   //时间差的毫秒数
                        //计算相差的年数
                        var years = Math.floor(date3 / (12 * 30 * 24 * 3600 * 1000));
                        //计算相差的月数
                        var leave = date3 % (12 * 30 * 24 * 3600 * 1000);
                        var months = Math.floor(leave / (30 * 24 * 3600 * 1000));
                        //计算出相差天数
                        var leave0 = leave % (30 * 24 * 3600 * 1000);
                        var days = Math.floor(leave0 / (24 * 3600 * 1000));
                        //计算出小时数
                        var leave1 = leave0 % (24 * 3600 * 1000);     //计算天数后剩余的毫秒数
                        var hours = Math.floor(leave1 / (3600 * 1000));
                        //计算相差分钟数
                        var leave2 = leave1 % (3600 * 1000);         //计算小时数后剩余的毫秒数
                        var minutes = Math.floor(leave2 / (60 * 1000));
                        //计算相差秒数
                        var leave3 = leave2 % (60 * 1000);       //计算分钟数后剩余的毫秒数
                        var seconds = Math.round(leave3 / 1000);
                        $scope.flightBackResult.FlightList[cabinNum].flighttimelist=hours+"小时"+minutes+"分钟";
                        cabinNum++;
                    }


                    if ($scope.flightBackResult.FlightList.length <= 0) {
                        $(".noflightsBack").show();
                        $(".airlineBack").hide();
                    }
                }

            } else {
                $(".loadingBack").hide();
                $(".noflightsBack").show();
                $(".airlineBack").hide();
                $(".datesBack").show();


            }
            GetFlightDaylistBack(queryFliInfo.AirportFrom, queryFliInfo.AirportTo, new Date(queryFliInfo.DepartureTime.replace(/-/g, "/")));
        });
    }

}]);